<template>
  <div class="my-container">
    <div class="header not-login" v-if="!userInfo">
      <div class="login-btn" @click="$router.push('/login')">
        <img
            class="mobile-img"
            src="~@/assets/mobile.png"
            @click="$router.push('/login')"
        >
        <span class="text">登录/注册</span>
      </div>
    </div>

    <!-- 已登录 -->
    <div class="userInfo header" v-else>
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image
              fit="cover"
              :src="userDetailInfo.photo"
              class="avatar"
              round
          />
          <span class="name">黑马头条</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{userDetailInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userDetailInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userDetailInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userDetailInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
<!--        <div>234</div>-->
      <van-grid clickable :column-num="2" class="nav-grid">
        <van-grid-item  to="/" >
          <template #icon>
            <i class="toutiao toutiao-shoucang"></i>
          </template>

          <template #text>
            <span class="text">收藏</span>
          </template>
        </van-grid-item>
        <van-grid-item url="/vant/mobile.html" >
          <template #icon>
            <i class="toutiao toutiao-lishi"></i>
          </template>
          <template #text>
            <span class="text">历史</span>
          </template>
        </van-grid-item>
      </van-grid>
    </div>
<!--    收藏历史模块-->

<!--    单元格导航-->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell @click="logOut" v-if="userInfo" class="logout-cell" title="退出登录" center/>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user'

export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      userDetailInfo: {}
    }
  },
  computed: {
    // 通过计算属性获取当前用户信息对象
    userInfo () {
      // vuex内部的user数据
      // 存在：登录
      // 不存在：未登录
      return this.$store.state.user
    }
  },
  watch: {},
  created () {
  //  用户是否登录
    if (this.userInfo) {
      this.handleGetUserInfo()
    }
  },
  mounted () {
    console.log(this.userInfo)
  },
  methods: {
    async handleGetUserInfo () {
      const res = await getUserInfo()
      this.userDetailInfo = res.data.data
      console.log(res)
    },
    logOut () {
      // confirm返回的是一个promise
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出？'
      })
        .then(() => {
        //  点击了确认
          console.log('确认')
          //  清除token
          this.$store.commit('setUser', null)
        })
        .catch(() => {
        //  点击了取消
          console.log('取消')
        })
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  > .header {
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    &.not-login {
      height: 361px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .mobile-img {
      width: 132px;
      height: 132px;
    }

    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;

      .text {
        font-size: 30px;
        color: #fff;
        margin-top: 15px;
      }
    }
  }
}

.userInfo {
  background: red;
  .base {
    height: 231px;
    padding: 76px 32px 23px;
    //background: #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;

    .name {
      color: #fff;
    }
    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 2px solid #fff;
      }
      .name {
        font-size: 29px;
        margin-left: 22px;
      }
    }
  }
}

.data {
  display: flex;
  justify-content: space-around;
  .data-item {
    height: 130px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #fff;
    span:nth-child(1) {
      font-size: 36px;
      margin-bottom: 15px;
    }
    span:nth-child(2) {
      font-size: 23px;
    }
  }
}

.nav-grid {
  background: #fff;
  .toutiao {
    font-size: 45px;
  }
  .toutiao-shoucang {
    color: #EB5253;
  }
  .toutiao-lishi {
    color: #FF9D1D;
  }
  .text {
    color: #000;
    margin-top: 8px;
    font-size: 28px;
  }
}

.logout-cell {
  height: 100px;
  text-align: center;
  color: #EB5253;
  margin: 10px 0;
  font-size: 30px;
}

</style>
